// 面试题 2: 实现 SIngleton 模式
// 题目：设计一个类，只能生成该类的一个实例。
// 考点：如何高效实现多线程环境下的单例模式，减少加锁，按需创建实例。

using System;

namespace Singleton
{
    // 只适用于单线程
    public sealed class Singleton1
    {
        private Singleton1()
        {
        }

        private static Singleton1 instance = null;
        public static Singleton1 Instance
        {
            get
            {
                if (instance == null)
                    instance = new Singleton1();
                return instance;
            }
        }
    }

    // 适用于多线程，但每次访问实例都需要加锁，效率不高
    public sealed class Singleton2
    {
        private Singleton2()
        {
        }

        private static readonly object syncObj = new object();

        private static Singleton2 instance = null;
        public static Singleton2 Instance
        {
            get
            {
                lock (syncObj)
                {
                    if (instance == null)
                        instance = new Singleton2();
                }
                return instance;
            }
        }
    }

    // 适用于多线程，通过加锁前后判断实例是否已创建大大提升了效率
    public sealed class Singleton3
    {
        private Singleton3()
        {
        }

        private static readonly object syncObj = new object();

        private static Singleton3 instance = null;
        public static Singleton3 Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (syncObj)
                    {
                        if (instance == null)
                            instance = new Singleton3();
                    }
                }
                return instance;
            }
        }
    }

    // 适用于多线程，利用了 C# 语言特性，但初次访问类型时便会创建实例，而非按需创建实例
    public sealed class Singleton4
    {
        private Singleton4()
        {
            Console.WriteLine("an instance of Single4 is created.");
        }

        private static Singleton4 instance = new Singleton4();
        public static Singleton4 Instance
        {
            get
            {
                return instance;
            }
        }

        public static void Print()
        {
            Console.WriteLine("Singleton4 print.");
        }
    }

    // 适用于多线程，利用了 C# 语言特性且按需创建实例
    public sealed class Singleton5
    {
        private Singleton5()
        {
            Console.WriteLine("an instance of Single5 is created.");
        }

        private static Singleton5 Instance
        {
            get
            {
                return Nested.instance;
            }
        }

        class Nested
        {
            static Nested()
            {
            }

            internal static readonly Singleton5 instance = new Singleton5();
        }

        public static void Print()
        {
            Console.WriteLine("Singleton5 print.");
        }
    }

    class Program
    {
        static void Main()
        {
            Singleton4.Print();
            Singleton5.Print();
            // 打印
            // an instance of Singleton4 is created.
            // Singleton4 print.
            // Singleton5 print.
        }
    }
}

